﻿	<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core'%>
	<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
	
	<script type="text/javascript">
		function reloadExpenseType() 
		{
			loadGridData ('#dg-accountingType', '<c:url value='/accountingType/getAllModel.html' />');
		}
		function formatter_accountingMode(value,row,index)
		{
			if('IN_COME' == value) 
			{
				return '收入';
			}
			if('OUT_LAY' == value) 
			{
				return '支出';
			}
			return '';
		}
		function formatter_accountingTypeAction (value,row,index) 
		{
			if (row.editing){
				if(row.id == '') 
				{
					var s = '<a href="#" style="text-decoration:underline" onclick="saverow(this)">保存</a> ';
					var d = '<a href="#" style="text-decoration:underline" onclick="deleterow(this)">删除</a>';
					return s+d;
				}
				else 
				{
					var s = '<a href="#" style="text-decoration:underline" onclick="saverow(this,' + row.id + ')">保存</a> ';
					var c = '<a href="#" style="text-decoration:underline" onclick="cancelrow(this)">取消</a>';
					return s+c;
				}
			} else {
				var e = '<a href="#" style="text-decoration:underline" onclick="editrow(this)">编辑</a> ';
				var d = '<a href="#" style="text-decoration:underline" onclick="deleterow(this,' + row.id + ')">删除</a>';
				return e+d;
			}
		}
		var editingIndex;
		var rowId;
		var accountingType_onBeforeEdit = function(index,row){
            row.editing = true;
			editingIndex = index;
			rowId = row.id;
            updateActions(index);
        }
		var accountingType_onAfterEdit = function(index,row){
            row.editing = false;
			editingIndex = undefined;
			rowId = '';
            updateActions(index);
        }
		var accountingType_onCancelEdit = function(index,row){
            row.editing = false;
			editingIndex = undefined;
			rowId = '';
            updateActions(index);
        }
		function updateActions(index){
			$('#dg-accountingType').datagrid('updateRow',{
				index: index,
				row:{}
			});
		}
		function getRowIndex(target){
			var tr = $(target).closest('tr.datagrid-row');
			return parseInt(tr.attr('datagrid-row-index'));
		}
		function editrow(target){
			if(editingIndex == undefined) 
			{
				$('#dg-accountingType').datagrid('beginEdit', getRowIndex(target));
			}
		}
		function deleterow(target, rowId){
			if(rowId == undefined) 
			{
				$('#dg-accountingType').datagrid('deleteRow', getRowIndex(target));
			}
			else 
			{
				if(editingIndex == undefined) 
				{
					$.messager.confirm('确认','您确认要删除?',function(r){
						if (r){
							$('#dg-accountingType').datagrid('deleteRow', getRowIndex(target));
							//console.log(rowId);
							ajaxPostRequest ('<c:url value='/accountingType/deleteModels.html' />', {ids:rowId}, reloadExpenseType);
						}
					});
				}
			}
		}
		function saverow(target, rowId){
			//console.log(rowId);
			if ($('#dg-accountingType').datagrid('validateRow', getRowIndex(target)))
			{
				var accountingModeEditor = $('#dg-accountingType').datagrid('getEditor', {index:getRowIndex(target), field:'accountingMode'});
				var accountingTypeNameEditor = $('#dg-accountingType').datagrid('getEditor', {index:getRowIndex(target), field:'name'});
				var accountingMode = $(accountingModeEditor.target).combobox('getValue');
				var accountingTypeName = $(accountingTypeNameEditor.target).val();
				//console.log(accountingMode);
				//console.log(accountingTypeName);
				$('#dg-accountingType').datagrid('endEdit', getRowIndex(target));
				if(rowId == undefined) 
				{
					ajaxPostRequest ('<c:url value='/accountingType/addModel.html' />', {id:rowId, accountingMode:accountingMode, name:accountingTypeName}, reloadExpenseType);
				}
				else 
				{
					ajaxPostRequest ('<c:url value='/accountingType/updateModel.html' />', {id:rowId, accountingMode:accountingMode, name:accountingTypeName}, reloadExpenseType);
				}
			}
		}
		function cancelrow(target){
			$('#dg-accountingType').datagrid('cancelEdit', getRowIndex(target));
		}
		function insert(){
			if(editingIndex == undefined) 
			{
				var row = $('#dg-accountingType').datagrid('getSelected');
				if (row){
					var index = $('#dg-accountingType').datagrid('getRowIndex', row);
				} else {
					index = 0;
				}
				$('#dg-accountingType').datagrid('insertRow', {
					index: index,
					row:{
						id:''
					}
				});
				$('#dg-accountingType').datagrid('selectRow',index);
				$('#dg-accountingType').datagrid('beginEdit',index);
			}
		}
		$.extend($.fn.validatebox.defaults.rules, {
			remoteInlineCheckExist: {
				validator: function(value, param){
					var datagridId = '#dg-accountingType';
					var idField = 'expenseTypeId';
					var nameField = 'name';
					var data={};
					data[nameField]=value;
					data[idField] = rowId;
					//var nameEditor = $(datagridId).datagrid('getEditor', {index:editingIndex, field:nameField});
					//data[nameField] = $(nameEditor.target).val();
					//console.log(data);
					var _3ee=$.ajax({url:"<c:url value='/accountingType/checkExist.html' />",dataType:"json",data:data,async:false,cache:false,type:"post"}).responseText;
					return _3ee=="true";
				},
				message: remoteMessage
			}
		});
	</script>
	
	<table id="dg-accountingType" title="<spring:message code="navigation.expenseTypeManagement" />" class="easyui-datagrid"
		style="" url="<c:url value='/accountingType/getAllModel.html' />"
		toolbar="#toolbar-expenseType2" pagination="false" rownumbers="true" singleSelect="true" 
		data-options="fit:true, fitColumns:true, sortName:'accountingMode', sortOrder:'desc', onBeforeEdit:accountingType_onBeforeEdit, onAfterEdit:accountingType_onAfterEdit, onCancelEdit:accountingType_onCancelEdit">
		<thead>
			<tr>
				<th field="id" data-options="hidden:true"><spring:message code="id" /></th>
				<th field="accountingMode" width="30" data-options="
                       sortable:true,
                       formatter: formatter_accountingMode,
                       editor:{
                           type:'combobox',
                           options:{
                               valueField:'value',
                               textField:'label',
								panelHeight: 'auto',
                               data:[{label:'收入',value:'IN_COME'},{label:'支出',value:'OUT_LAY'}],
                               required:true
                           }
                       }">模式</th>
				<th field="name" width="50" data-options="sortable:false, editor:{type:'validatebox', options:{required:true, validType:'remoteInlineCheckExist[]'}}"><spring:message code="name" /></th>
				<th field="action" width="50" data-options="formatter:formatter_accountingTypeAction">操作</th>
			</tr>
		</thead>
	</table>
	
	<div id="toolbar-expenseType2" style="height:auto">
        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="insert()" title="<spring:message code="newType" />"></a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-reload', plain:true" onclick="reloadExpenseType()" title="<spring:message code="refresh" />"></a>
    </div>
